//
// Created by ECNU on 2024/2/22.
//

#ifndef NANO_FAISS_FAISSASSERT_H
#define NANO_FAISS_FAISSASSERT_H

#include <nanofaiss/impl/FaissException.h>
#include <nanofaiss/impl/platform_macros.h>
#include <cstdio>
#include <cstdlib>
#include <string>


#define FAISS_THROW_MSG(MSG)                                   \
    do {                                                       \
        throw faiss::FaissException(                           \
                MSG, __PRETTY_FUNCTION__, __FILE__, __LINE__); \
    } while (false)

#define FAISS_THROW_FMT(FMT, ...)                              \
    do {                                                       \
        std::string __s;                                       \
        int __size = snprintf(nullptr, 0, FMT, __VA_ARGS__);   \
        __s.resize(__size + 1);                                \
        snprintf(&__s[0], __s.size(), FMT, __VA_ARGS__);       \
        throw faiss::FaissException(                           \
                __s, __PRETTY_FUNCTION__, __FILE__, __LINE__); \
    } while (false)

#define FAISS_THROW_IF_NOT(X)                          \
    do {                                               \
        if (!(X)) {                                    \
            FAISS_THROW_FMT("Error: '%s' failed", #X); \
        }                                              \
    } while (false)


#endif // NANO_FAISS_FAISSASSERT_H
